<?php

abstract class AbstractController
{

  protected $request;

  protected $response;

  protected $router;

  /**
   *
   * @return Config
   */
  public function config()
  {
    global $config;
    return $config;
  }

  /**
   *
   * @return Request
   */
  public function request($request = null)
  {
    if (! empty($request)) {
      $this->request = $request;
    }

    return $this->request;
  }

  /**
   *
   * @return Response
   */
  public function response($response = null)
  {
    if (! empty($response)) {
      $this->response = $response;
    }

    return $this->response;
  }

  /**
   *
   * @return Router
   */
  public function router($router = null)
  {
    if (! empty($router)) {
      $this->router = $router;
    }

    return $this->router;
  }

  protected function initialize()
  {
    //
  }

  protected function callAction($action)
  {
    $func = Inflector::variablize($action . 'Action');

    if (! method_exists($this, $func)) {
      throw new Exception('Controller action not found');
    }

    $this->action = $action;

    $this->beforeAction($action);

    $output = call_user_func(array($this, $func));

    $this->afterAction($action, $output);

    return $output;
  }

}
